/*
 * Copyright (C) 2010, Funcom AS, All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  @authors
 *  Svein Gunnar Larsen <sveingunnarlarsen@gmail.com>
 *  Erik Thune Lund <culex.eriklund@gmail.com>
 *  Michel Krohn-Dale <kronicz@gmail.com>
 */

package com.funcom.peeler.component;

import com.funcom.peeler.ComponentUtils;
import com.funcom.peeler.NewAbsoluteLayout;
import com.funcom.peeler.peelable.PeelComponent;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jmex.bui.BComponent;
import com.jmex.bui.BContainer;
import com.jmex.bui.BLabel;
import com.jmex.bui.background.TintedBackground;
import com.jmex.bui.util.Rectangle;

import java.util.Collection;

public class CanvasWindow extends BContainer {

    BLabel titlebar = new BLabel( "Canvas", "statusbar" );

    public CanvasWindow() {
        super();
        setSize( 300, 300 );
        setName("canvas_window");
        setLayoutManager( new NewAbsoluteLayout() );

        setStyleClass("canvas");

        titlebar.setName("titlebar");
        titlebar.setSize( getWidth(), 20 );
        titlebar.setLocation( 0, getHeight() );
        titlebar.setVisible( true );
        titlebar.setBackground( 0, new TintedBackground( new ColorRGBA( 0.5f, 0, 0, 1 ) ) );
    }

    /* Keep the CanvasWindow in the center of the workspace( or whichever is its parent ) */
    public void setBounds(int x, int y, int width, int height) {

        if ( getParent() != null ) {
            x = (getParent().getBounds().width / 2) - (width / 2);
            y = (getParent().getBounds().height / 2) - (height / 2);
        }

        super.setBounds(x, y, width, height);

        // Make sure the titlebar is always at the top of the canvas
        titlebar.setBounds( 0, getHeight(), getWidth(), titlebar.getHeight() );

        for ( int i = 0 ; i < getComponentCount() ; i++ ) {

            BComponent component = getComponent(i);

            if ( component == titlebar ) continue;

            Rectangle new_bounds = ComponentUtils.enforceParentBounds( this.getBounds(), component.getBounds() );

            int titlebar_diff = new_bounds.y + new_bounds.height - titlebar.getY();

            if ( titlebar_diff > 0 )
                new_bounds.y -= titlebar_diff;

            component.setBounds( new_bounds.x, new_bounds.y, new_bounds.width, new_bounds.height );
        }
    }

    public void renderComponent( Renderer renderer ) {
        super.renderComponent( renderer );
        titlebar.setBounds( 0, getHeight(), getWidth(), titlebar.getHeight() );
        titlebar.render( renderer );
    }

    public BComponent getHitComponent( int mx, int my ) {

        BComponent hit_component = super.getHitComponent( mx, my );

        if ( hit_component == titlebar ) return this;

        return hit_component;
    }

    protected void wasAdded() {
        super.wasAdded();

        Rectangle bounds = getBounds();
        setBounds( bounds.x, bounds.y, bounds.width, bounds.height );
    }

    public void resizeToChildren( Collection<PeelComponent> children ) {

        for ( PeelComponent child : children ) {

            BComponent component  = child.getComponent();

            // Resize the canvas to fit as new components come in
            int component_right = component.getX() + component.getWidth();
            int component_top = component.getY() + component.getHeight();

            setSize( ( ( getWidth() > component_right ) ? getWidth() : component_right ), getHeight() );
            setSize( getWidth(), ( ( getHeight() > component_top ) ? getHeight() : component_top ) );
        }
    }
}


